using System;
using System.Collections.Generic;
using System.Data;
using MallInteractiveMap.ClientDataHandler;
using MallInteractiveMap.Common;
using MallInteractiveMap.DTO;
using System.Linq;

namespace MallInteractiveMap.CMSWebClient.Presenter
{
	public class UserPresenter
	{
		public UserPresenter()
		{
		}
		
		public List<DTOUser> GetUsers(int Id)
		{
			string error = string.Empty;
			QueryParamList param = new QueryParamList();
			param.Add(new QueryParamObj() { ParamName = "Id", DBType = DbType.Int32 , ParamValue = Id});
			List<DTOUser>user= ClientData.GetUsers(param, ref error);
			if (!string.IsNullOrEmpty(error))
			{

			}
			return user;
		}
        public bool IsValidUser(string loginId, string password)
        {
            string error = string.Empty;
            QueryParamList param = new QueryParamList();
            param.Add(new QueryParamObj() { ParamName = "LoginId", DBType = DbType.String, ParamValue = loginId });
            param.Add(new QueryParamObj() { ParamName = "Password", DBType = DbType.String, ParamValue = password });
            bool isValid = ClientData.IsValidUser(param, ref error);
            if (!string.IsNullOrEmpty(error))
            {

            }
            return isValid;
        }
        public DTOUser GetUser(string loginId)
        {
            string error = string.Empty;
            QueryParamList param = new QueryParamList();
            param.Add(new QueryParamObj() { ParamName = "LoginId", DBType = DbType.String, ParamValue = loginId });
            List<DTOUser> users = ClientData.GetUsers(param, ref error);
            if (!string.IsNullOrEmpty(error))
            {
                
            }
            return users.FirstOrDefault();
        }
		public bool SetDataUser(DTOUser oUser)
		{
			QueryParamList param = new QueryParamList();
			param.Add(new QueryParamObj(){ParamName="Id", DBType=DbType.Int32, ParamValue= oUser.Id});
			param.Add(new QueryParamObj(){ParamName="Name", DBType=DbType.String, ParamValue= oUser.Name});
			param.Add(new QueryParamObj(){ParamName="LoginId", DBType=DbType.String, ParamValue= oUser.LoginId});
			param.Add(new QueryParamObj(){ParamName="Email", DBType=DbType.String, ParamValue= oUser.Email});
			param.Add(new QueryParamObj(){ParamName="SecretAnswer", DBType=DbType.String, ParamValue= oUser.SecretAnswer});
			param.Add(new QueryParamObj(){ParamName="LastUpdated", DBType=DbType.DateTime, ParamValue= oUser.LastUpdated});
			string error = string.Empty;
			string spName = oUser.IsNew ? "INSERTUser " : "UPDATEUser";
			bool isSuccess = ClientData.SetData(param, spName, ref error);
			return string.IsNullOrEmpty(error) & isSuccess;
		}
		public bool SetDataUser(int Id)
		{
			string error = string.Empty;
			QueryParamList param = new QueryParamList();
			param.Add(new QueryParamObj() { ParamName = "Id", DBType = DbType.Int32 , ParamValue = Id});
			bool isSuccess = ClientData.SetData(param, "DELETEUser",  ref error);
			return string.IsNullOrEmpty(error) & isSuccess;
		}

	    public bool ChangedPassword(string currentPassword, string newPassword,  string loginId)
	    {
            if(!IsValidUser(loginId, currentPassword))
	        {
                return false;
	        }
	        string query = "Update [User] Set Password = '" + newPassword + "'";
            QueryParamList param = new QueryParamList();
            param.Add(new QueryParamObj() { ParamName = "Name", DBType = DbType.String, ParamValue = currentPassword });
            param.Add(new QueryParamObj() { ParamName = "LoginId", DBType = DbType.String, ParamValue = loginId });
	        string pError = string.Empty;
	        ClientData.ExecuteQuery(query, param, ref pError);
            return true;
	    }
	}
}
